#include "operatelogapi.h"

OperateLogApi::OperateLogApi(QObject *parent)
    : QObject{parent}
{
    baseUrl = Config::baseUrl;
    manager = new QNetworkAccessManager(0);
    request.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("application/json"));//post请求设置为json，才能发送josn格式文件
    request.setRawHeader("Authorization",  qApp->property("token").value<QString>().toLocal8Bit());

}

QByteArray *OperateLogApi::list(int pageNum, int pageSize,QString sysMoudle,QString operateUser,QString type,
                                QString status,QString start,QString end)
{
    QString params = "monitor/operlog/list?pageNum="+ QString::number(pageNum) +
                     "&pageSize=" + QString::number(pageSize);
    if(!sysMoudle.isEmpty()){
        params += "&title=" + sysMoudle;
    }
    if(!operateUser.isEmpty()){
        params += "&operName=" + operateUser;
    }
    if(!type.isEmpty()){
        params += "&businessType=" + type;
    }
    if(!status.isEmpty()){
        params += "&status=" + status;
    }
    if(start != "2000/1/1" && end != "2000/1/1"){
        params += "&params%5BbeginTime%5D=" + start + "&params%5BendTime%5D=" + end;
    }
    request.setUrl(baseUrl + params);
    QNetworkReply *reply= manager->get(request);
    //同步请求
    QEventLoop eventLoop;
    connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(reply->readAll().data());
    reply->deleteLater();
    utils.requestCheck(data);
    return data;
}



QByteArray *OperateLogApi::clear()
{
    request.setUrl(baseUrl + "monitor/operlog/clean");
    QNetworkReply *reply = manager->deleteResource(request);
    //同步请求
    QEventLoop eventLoop;
    connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(reply->readAll().data());
    reply->deleteLater();
    utils.requestCheck(data);
    return data;
}

QByteArray *OperateLogApi::deleteItems(QString ids)
{
    request.setUrl(baseUrl + "monitor/operlog/" + ids);
    QNetworkReply * r = manager->deleteResource(request);
    //同步请求
    QEventLoop eventLoop;
    connect(r, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(r->readAll().data());
    r->deleteLater();
    utils.requestCheck(data);
    return data;
}

